In [1]:
class A:
pass
class B:
pass
class C(A,B):
pass
print(C.__bases__)
Check this article on python.org for more info. Part of this presentation also based on this tutorial.
C
, in the case of single inheritance hierarchy, if C
is a subclass of C1
, and C1
is a subclass of C2
, then the linearization of C
is simply the list [C,C1,C2]
.C
.C
, including the class itself, ordered from the nearest ancestor to the furthest, is called the class precedence list or the linearization of C
.C
is also used as a synonymous for the linearization of C
.C1
precedes C2
in the linearization of C
then C1
precedes C2
in the linearization of any subclass of C
.class X(O), class Y(O), class A(X,Y), class B(Y,X), class C(B,A)
C
from A
and B
since X
precedes Y
in A
, but Y
precedes X
in B
, therefore the method resolution order would be ambiguous in C
(XY
breaks monotonicity with B
, YX
breaks monotonicity with A
).
C1 C2 ... CN
indicates the list of classes [C1,C2,...,CN]
C1
C2 ... CN
[C] + [C1,C2,...,CN] = C + (C1 C2 ... CN) = C C1 C2 ... CN
C
in a multiple inheritance hierarchy, with C
inheriting from the base classes B1, B2, ..., BN
:C
is the sum of C
plus the merge of linearizations of the parents and the list of the parentsL[C(B1 ... BN)] = C + merge(L[B1],...,L[BN], B1 ... BN)
L[Y(X1 X2 X3)] = Y + merge(L[X1],L[X2],L[X3], X1 X2 X3)
Consider a simple merge example: merge(DO,EO,DE) = DEO
L[C(B1 ... BN)] = C + merge(L[B1],...,L[BN], B1 ... BN)
L[O] = O
L[D] = D + merge(L[O],O) = D + merge(O,O) = DO
L[E] = EO, L[F] = FO
L[B] = B + merge(L[D],L[E],DE)
= B + merge(DO,EO,DE)
= B + D + merge(O,EO,E)
= B + D + E + merge(O,O)
= BDEO
L[C] = C + merge(L[D],L[F],DF)
= C + merge(DO,FO,DF)
= CDFO
L[A] = A + merge(L[B],L[C],BC)
= A + merge(BDEO,CDFO,BC)
= A + B + merge(DEO,CDFO,C)
= A + B + C + merge(DEO,DFO)
= A + B + C + D + merge(EO,FO)
= A + B + C + D + E + merge(O,FO)
= A + B + C + D + E + F + merge(O,O)
= ABCDEFO
In [2]:
class F: pass
class E: pass
class D: pass
class C(D,F): pass
class B(D,E): pass
class A(B,C): pass
from inspect import getmro
print(getmro(A))
print(A.__mro__)
In [3]:
class A: pass
class B: pass
class C(A,B): pass
class D(B,A): pass
class E(C,D): pass
print(E.__mro__)